<?php
/**
 * Created by PhpStorm.
 * User: front
 * Date: 2018/9/11
 * Time: 15:33
 */

namespace App\Services;


use JPush\Client;

class Pusher
{
    private static function init()
    {
        $appKey = env('PUSHER_APP_KEY');
        $secret = env('PUSHER_APP_SECRET');

        return new Client($appKey, $secret);
    }

    /**
     * 系统通知
     * @param string $registration_id
     * @param string $alert
     */
    public static function alert($registration_id = '', $alert = '')
    {
        if ($registration_id) {
            $cid = md5(microtime() . $registration_id);
            $client = static::init();
            $pusher = $client->push();
            $pusher//->setCid($cid)
                ->setPlatform('all')
                ->addRegistrationId($registration_id)
                ->setNotificationAlert($alert)
                ->send();
        }
    }

    /**
     * 内部消息
     * @param string $registration_id
     * @param string $content
     * @param array $extra
     */
    public static function message($registration_id = '', $content = '', $extra = [])
    {
        if ($registration_id) {
//            $cid = md5(microtime() . $registration_id);
            $msg = [];
            if (!empty($extra)) {
                $msg['extras'] = $extra;
            }
            $client = static::init();
            $pusher = $client->push();
            $pusher//->setCid($cid)
                ->setPlatform('all')
                ->addRegistrationId($registration_id)
                ->message($content, $msg)
                ->send();
        }
    }
}